package prototype

import (
	"encoding/json"
	"strings"
)

// 用户工厂的全局单例
var DefaultUserFactory IUserFactory = newUserFactory()

type IUserFactory interface {
	Create() *UserInfo
}

type tUserFactory struct {
	defaultUserInfo *UserInfo
}

// 创建用户工厂实例
func newUserFactory() *tUserFactory {
	reader := strings.NewReader(loadUserConfig())
	decoder := json.NewDecoder(reader)
	user := newEmptyUser()
	e := decoder.Decode(user)
	if e != nil {
		panic(e)
	}

	return &tUserFactory{
		defaultUserInfo: user,
	}
}

// 加载默认用户的属性配置
func loadUserConfig() string {
	return `{
	"GetID": 0,
	"Name" : "新用户",
	"RoleList" : ["guest"]
}`
}

func (me *tUserFactory) Create() *UserInfo {
	return me.defaultUserInfo.Clone().(*UserInfo)
}
